<?php

namespace app\common\validate;

use think\Validate;

class UserAccount extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
    	'user_id' => 'require',
	    'name' => 'require',
	    'account_type_id' => 'checkAccountType',
	    'balance' => 'require|float',
    ];
    /**
     * 提示消息
     */
    protected $message = [
    	'user_id.require' => '用户不能为空',
	    'name.require' => '帐户名称不能为空',
	    'balance.require' => '请填写余额',
	    'balance.float' => '余额只能是数字'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => [],
        'edit' => [],
    ];
	
	
	/**
	 * 检查帐号类型是否存在
	 * @param $value
	 * @return bool|string
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
    protected function checkAccountType($value) {
    	$model = new \app\common\model\AccountType();
        $row = $model->where(['id' => $value, 'status' => $model::STATUS_Y])->find();
        if(!$row) {
        	return '帐号类型不存在';
        }
    	return true;
    }
}
